from functools import wraps
from flask_login import current_user
from flask import flash, redirect, url_for, request
import queue

queue_message = queue.Queue()


def put_queue(task_id):
    # from random import randint
    from time import sleep
    for i in range(1, 101):
        queue_message.put({'task_id': task_id, 'progress': i})
        print(f'任务ID：{task_id}，进度：{i}')
        sleep(0.5)
    queue_message.put({'task_id': task_id, 'progress': 100, 'finish': True})


def authority_(a=0):
    """
    权限验证装饰器
    :param a: 权限等级
    """
    def vail_authority(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            if current_user.authority == a:
                return func(*args, **kwargs)
            else:
                flash('权限不足，无法访问！', 'error')
                return redirect(url_for('admin.index'))

        return wrapper

    return vail_authority


# 过去头部信息的IP地址
def get_ip():
    return request.headers.get('X-Real-IP') or request.headers.get('X-Forwarded-For') or request.remote_addr
